/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package net.octopeer.core;

import java.io.File;
import java.io.StringReader;

import javax.xml.transform.Result;
import javax.xml.transform.Source;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;
import org.w3c.dom.Text;

import org.xml.sax.InputSource;

import net.octopeer.utilities.XMLTools;
import net.octopeer.utilities.SimpleSHA1;

/**
 *
 * @author andria
 */
public class PeerNode {
    private String nodeID;
    private String nodeIPaddress;
    private int nodePort;
    
    public PeerNode() {
        //
    }
    
    public PeerNode(String nodeIPaddress, int nodePort) throws Exception {
        this.nodeIPaddress = nodeIPaddress;
        this.nodePort = nodePort;
        this.nodeID = SimpleSHA1.SHA1(nodeIPaddress + ":" + nodePort);
    }
    
    public void setNodeID(String nodeID) {
        this.nodeID = nodeID;
    }
    
    public void setNodeIPaddress(String nodeIPaddress) {
        this.nodeIPaddress = nodeIPaddress;
    }
    
    public void setNodePort(int nodePort) {
        this.nodePort = nodePort;
    }
    
    public String getNodeID() {
        return this.nodeID;
    }
    
    public String getNodeIPaddress() {
        return this.nodeIPaddress;
    }
    
    public int getNodePort() {
        return this.nodePort;
    }
    
    public Document toXMLDoc() throws Exception {
        Document doc;
        XMLTools xt = new XMLTools();        
        doc = xt.db.newDocument();
        
        Element rootEle = doc.createElement("Node");

        // ID
        Element nodeIDEle = doc.createElement("Node:ID");
        Text nodeIDText = doc.createTextNode(this.nodeID);
        nodeIDEle.appendChild(nodeIDText);
        rootEle.appendChild(nodeIDEle);        
        
        // IPaddress
        Element nodeIPaddressEle = doc.createElement("Node:IPaddress");
        Text nodeIPaddressText = doc.createTextNode(this.nodeIPaddress);
        nodeIPaddressEle.appendChild(nodeIPaddressText);
        rootEle.appendChild(nodeIPaddressEle);        
        
        // Port
        Element nodePortEle = doc.createElement("Node:Port");
        Text nodePortText = doc.createTextNode(Integer.toString(this.nodePort));
        nodePortEle.appendChild(nodePortText);
        rootEle.appendChild(nodePortEle);        
        
        doc.appendChild(rootEle);                
        
        return doc;
    }
    
    public String toXMLString() throws Exception {
        String result = null;
        
        XMLTools xt = new XMLTools();                
        result = xt.writeXMLString(this.toXMLDoc());
        
        return result;
    }
    
    public void toXMLFile(String fileName) throws Exception {
        // Prepare the DOM document for writing
        Source source = new DOMSource(this.toXMLDoc());

        // Prepare the output file
        File file = new File(fileName);
        Result result = new StreamResult(file);

        // Write the DOM document to the file
        Transformer xformer = TransformerFactory.newInstance().newTransformer();
        xformer.transform(source, result);
    }
    
    public PeerNode fromXMLDoc(Document dom) {
        PeerNode result = new PeerNode();
        
        Element rootEle = dom.getDocumentElement();
        
        if (rootEle.getTagName().equals("Node")) {
            NodeList nodeIDList = rootEle.getElementsByTagName("Node:ID");
            String nodeIDText = nodeIDList.item(0).getFirstChild().getNodeValue();
            NodeList nodeIPaddressList = rootEle.getElementsByTagName("Node:IPaddress");
            String nodeIPaddressText = nodeIPaddressList.item(0).getFirstChild().getNodeValue();
            NodeList nodePortList = rootEle.getElementsByTagName("Node:Port");
            String nodePortText = nodePortList.item(0).getFirstChild().getNodeValue();
            result.setNodeID(nodeIDText);
            result.setNodeIPaddress(nodeIPaddressText);
            result.setNodePort(Integer.valueOf(nodePortText));
    
        }
                
        return result;
    }
    
    public PeerNode fromXMLString(String data) throws Exception {
        Document dom = null;
        XMLTools xt = new XMLTools();
        PeerNode result = null;
        
        // Parse the XML stream input to Document type
        dom = xt.db.parse(new InputSource(new StringReader(data)));
        
        result = fromXMLDoc(dom);
        
        return result;
    }
    
    public PeerNode fromXMLFile(String fileName) throws Exception {
        Document dom = null;
        File file = null;
        PeerNode result = null;
        XMLTools xt = new XMLTools();
        
        // Parse the XML stream input to Document type
        file = new File(fileName);
        dom = xt.db.parse(file);
        
        result = fromXMLDoc(dom);
        
        return result;
    }
    
//    public static void main(String[] args) throws Exception {
//        Node n1 = new Node("192.168.0.1", 9090);
//        System.out.print(n1.toXMLString());
//        n1.toXMLFile("n1.xml");
//        
//        Node n2 = new Node();
//        n2 = n2.fromXMLFile("n1.xml");
//        n2.toXMLFile("n2.xml");
//    }
}
